const fs = require('fs')
const url = require('url')
const express = require('express')
const movieModel = require('./model/movieModel')
const userModel = require('./model/userModel')
const splider = require('./splider')

//创建路由
const router = express.Router()
router.get('/homeSwiper', (req, response) => {
    splider.Splider('https://m.v.qq.com/x/m/channel/figure/tv').then($ => {
        let duqu = $('.swiper-wrapper ').eq(1).find('.swiper-slide')
        let results = []
        duqu.each(function (i, elem) {
            let imgUrl = $(this).find('a').find('.item_content').find('img').attr('dsrc')
            results.push({
                imgUrl: imgUrl
            })
        })
        response.send(results)
    })

})
router.get('/movies', (req, res) => {
    fs.readFile('./movies.json', 'utf-8', (err, data) => {
        if (err) {
            console.log(err);
        }
        console.log(typeof data);

        res.send(JSON.parse(data))
    })
})
//处理首页电影分类数据请求
router.get('/top250', (req, res) => {
    fs.readFile('./top250.json', 'utf-8', (err, data) => {
        if (err) {
            console.log(err);
        }
        console.log(typeof data);
        res.send(JSON.parse(data))
    })
})
//处理搜索请求
router.get('/search', (req, response) => {
    let movieName = url.parse(req.url, true).query.movieName
    console.log(movieName);
    console.log(typeof movieName);
    let searchUrl = `https://m.douban.com/search/?query= ${movieName}`;
    let results = [];
    splider.Splider(searchUrl).then($ =>{
        let duqu = $('.search_results_subjects li ');
            duqu.each(function (i, elem) {
                let href = $(this).find('a').attr('href');
                let imgUrl = $(this).find('img').attr('src');
                let title = $(this).find('.subject-title').text();
                let rate = $(this).find('p').children('span').attr('data-rating');
                results.push({
                    href: 'https://m.douban.com' + href,
                    imgUrl: imgUrl,
                    title: title,
                    rate: rate / 10
                });
            });
            response.send(results);
    })

    
})
//处理分类数据请求
router.get('/type', (req, res) => {
    let geners = url.parse(req.url, true).query.geners
    console.log(geners);
    if (geners == '全部') {
        movieModel.find({}, (err, data) => res.send(data))
        return
    }
    movieModel.find({
        "genres": `${geners}`
    }, function (err, data) {
        if (err) {
            console.log(err);
        }
        if (data.length != 0) {
            res.send(data)
        } else {
            res.send('无数据')
        }
    })
})
//处理details页面演职人员数据请求
router.get('/roleList', (req, response) => {
    let movieId = url.parse(req.url, true).query.movieId
    let roleUrl = `https://movie.douban.com/subject/${movieId}/celebrities`;
    let results = []
    splider.Splider(roleUrl).then($ =>{
        let duqu = $('.list-wrapper').find('ul li')
        duqu.each(function (i, elem) {
            let actorLink = $(this).find('a').attr('href')
            let imgUrl = $(this).find('a').children('div').attr('style').slice(22, -1)
            let name = $(this).find('.name a').text().split(' ')
            let role = $(this).find('.role').text().split('(')
            results.push({
                actorLink: actorLink, //演职人员信息链接
                imgUrl: imgUrl, //演职人员照片链接
                name: name[0],
                role: role[0].slice(0, 2),
            })
        })
        response.send(results)
    })
  
})
router.post('/login', (req, res) => {
    let user = req.body.user
    let pass = req.body.pass
    userModel.find({
        user: `${user}`
    }, (err, ret) => {
        if (err) {
            res.send('登录失败，请检查你的网络')
        }
        if (ret.length != 0) {
            if (ret[0].user == user && ret[0].password == pass) {
                res.send({
                    msg: '登录成功',
                    code: 0
                })
            } else {
                res.send({
                    code: 1
                })
            }
        } else {
            res.send({
                code: 2
            })
        }
    })

})
//导出router
module.exports = router